package com.martini.satoken01.exception.handler;

import cn.dev33.satoken.exception.NotLoginException;
import com.martini.satoken01.constant.ResCodeConstant;
import com.martini.satoken01.model.AjaxJson;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author martini at 2021/12/17 13:41
 */
@ControllerAdvice
public class GlobalExceptionHandler {
    @ResponseBody
    @ExceptionHandler({Exception.class})
    public AjaxJson handleException(Exception e, HttpServletRequest request, HttpServletResponse response) {
        e.printStackTrace();
        if (e instanceof NotLoginException) {
            return AjaxJson.fail().setCode(ResCodeConstant.NOT_LOGIN).setMsg(e.getMessage());
        }
        return AjaxJson.fail().setMsg(e.getMessage());
    }
}
